﻿using System;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using visinia.SmartEngine.Providers;
using viWiki.Providers;

namespace viWiki
{
    public class SqlDataProvider : viWikiDataProvider
    {
        #region INIT
        private static string _sqlConnectionString = null;

        public SqlDataProvider()
        {
            _sqlConnectionString = ProviderController.GetDataProviderConnectionString();
        }
        public string ConnectionString
        {
            get { return _sqlConnectionString; }
        }
        #endregion
        
        #region WIKI PAGES
        public override IDataReader GetWikiPages()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viWikiGetWikiPages");
        }

        public override IDataReader AddWikiPage(int NamespaceInfoId, string Name, string Content, DateTime Date, string Author, string Url)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viWikiAddWikiPage", NamespaceInfoId, Name, Content, Date, Author, Url);
        }

        public override void UpdateWikiPage(int WikiPageId, int NamespaceInfoId, string Name, string Content, DateTime Date, string Author, string Url)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiUpdateWikiPage", WikiPageId, NamespaceInfoId, Name, Content, Date, Author, Url);
        }

        public override void DeleteWikiPage(int WikiPageId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiDeleteWikiPage", WikiPageId);
        }
        #endregion

        #region WikiSettings
        public override IDataReader GetWikiSettings()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viWikiGetWikiSettings");
        }

        public override void AddWikiSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiAddWikiSetting", key, value);
        }

        public override void UpdateWikiSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiUpdateWikiSetting", key, value);
        }

        public override void DeleteWikiSetting(string key)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiDeleteWikiSetting", key);
        }
        #endregion

        #region Namespaces
        public override IDataReader GetNamespaces()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viWikiGetNamespaces");
        }

        public override IDataReader AddNamespace(string Name, int DefaultWikiPageId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viWikiAddNamespace", Name, DefaultWikiPageId);
        }

        public override void UpdateNamespace(int NamespaceInfoId, string Name, int DefaultWikiPageId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiUpdateNamespace", NamespaceInfoId, Name, DefaultWikiPageId);
        }

        public override void DeleteNamespace(int NamespaceInfoId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiDeleteNamespace", NamespaceInfoId);
        }
        #endregion

        #region WIKI PAGE LINKS
        public override IDataReader GetWikiPageLinks()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viWikiGetWikiPageLinks");
        }

        public override void AddWikiPageLink(int SourceWikiPageId, int DestinationWikiPageId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiAddWikiPageLink", SourceWikiPageId, DestinationWikiPageId);
        }
        public override void DeleteWikiPageLinks(int SourceWikiPageId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viWikiDeleteWikiPageLinks", SourceWikiPageId);
        }
        #endregion        
    }
}
